/*
 * @Author: wangdazhuang
 * @Date: 2025-01-20 17:39:28
 * @LastEditTime: 2025-01-27 20:11:24
 * @LastEditors: wangdazhuang
 * @Description: 
 * @FilePath: /pornhub_app/lib/components/original_favor_cell/widget.dart
 */
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:pornhub_app/components/image_view.dart';
import 'package:pornhub_app/model/original_flavor_cell_model/original_flavor_cell_model.dart';
import 'dart:math' as math;

class OriginalFlavorCell extends StatefulWidget {
  const OriginalFlavorCell({super.key, required this.originalFlavor});

  final OriginalFlavorCellModel originalFlavor;

  @override
  State<OriginalFlavorCell> createState() => _OriginalFlavorCellState();
}

class _OriginalFlavorCellState extends State<OriginalFlavorCell> {
  /// 地址
  final String _address = '';

  /// 联系方式
  final String _contactDetails = '';

  /// 详细地址
  final String _detailAddress = '';

  /// 姓名
  final String _name = '';

  bool get _isNeedAddress =>
      _address.isNotEmpty &&
      _detailAddress.isNotEmpty &&
      _contactDetails.isNotEmpty &&
      _name.isNotEmpty;

  Map<String, String> get addressMap => {
        'address': _address,
        'detailAddress': _detailAddress,
        'contactDetails': _contactDetails,
        'name': _name,
      };

  /// 弹窗tag
  String get _dialogTagByConfirm => '${widget.originalFlavor.id}_confirm';

  /// 弹窗tag
  String get _dialogTagByPreview => '${widget.originalFlavor.id}_preview';

  /// 弹窗tag
  String get _dialogTagByAddress => '${widget.originalFlavor.id}_address';

  Widget _buildConfirmDialogContent() {
    return Container(
      color: const Color(0xff111111),
      padding: EdgeInsets.fromLTRB(20.w, 20.w, 20.w, 40.w),
      width: double.infinity,
      height: 360.w,
      child: Column(
        mainAxisAlignment: MainAxisAlignment.start,
        children: [
          Row(
            mainAxisAlignment: MainAxisAlignment.end,
            children: [
              GestureDetector(
                onTap: () {
                  SmartDialog.dismiss(tag: _dialogTagByConfirm);
                },
                child: Image.asset(
                  'assets/images/icons/ic_close_white.png',
                  width: 16.w,
                  height: 16.w,
                ),
              )
            ],
          ),
          SizedBox(height: 12.w),
          Row(
            children: [
              ClipRRect(
                borderRadius: BorderRadius.circular(12.w),
                child: ImageView(
                  src: widget.originalFlavor.images?[0] ?? '',
                  width: 74.w,
                  height: 84.w,
                ),
              ),
              SizedBox(width: 7.w),
              Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                mainAxisAlignment: MainAxisAlignment.end,
                children: [
                  Text(
                    widget.originalFlavor.title ?? '',
                    style: TextStyle(fontSize: 12.w, color: Colors.white),
                    maxLines: 2,
                    overflow: TextOverflow.ellipsis,
                  ),
                  SizedBox(height: 12.w),
                  Text(
                    '${widget.originalFlavor.price}金币',
                    style: TextStyle(
                        fontSize: 12.w, color: const Color(0xFFf43670)),
                  ),
                  SizedBox(height: 4.w),
                ],
              )
            ],
          ),
          SizedBox(height: 12.w),
          Container(
            width: double.infinity,
            padding: EdgeInsets.all(14.w),
            decoration: BoxDecoration(
              color: const Color(0xff484848),
              borderRadius: BorderRadius.circular(4.w),
            ),
            child: Row(
              children: [
                Expanded(
                  child: Text(
                    '添加收货地址',
                    style: TextStyle(fontSize: 12.w, color: Colors.white),
                  ),
                ),
                SizedBox(
                  width: 20.w,
                ),
                GestureDetector(
                  onTap: () {
                    // SmartDialog.dismiss(tag: _dialogTagByConfirm);
                    SmartDialog.show(
                      builder: (context) => _buildAddressDialogContent(),
                      tag: _dialogTagByAddress,
                      alignment: Alignment.center,
                    );
                  },
                  child: Transform.rotate(
                    angle: math.pi / 1, // 旋转角度 (以弧度为单位)
                    child: Image.asset(
                        'assets/images/icons/base_ic_back_white.png',
                        width: 9.w),
                  ),
                ),
              ],
            ),
          ),
          SizedBox(height: 32.w),
          Container(
            width: double.infinity,
            height: 41.w,
            decoration: BoxDecoration(
              color: const Color(0xffA62DE4),
              borderRadius: BorderRadius.circular(22.w),
            ),
            child: GestureDetector(
              onTap: () {
                // SmartDialog.dismiss(tag: _dialogTagByConfirm);
              },
              child: Center(
                child: Text(
                  '立即购买',
                  style: TextStyle(fontSize: 14.w, color: Colors.white),
                ),
              ),
            ),
          )
          // Text('立即购买', style: TextStyle(fontSize: 14.w, color: Colors.white)),
        ],
      ),
    );
  }

  Widget _buildAddressDialogContent() {
    return SafeArea(
      child: Scaffold(
        backgroundColor: const Color(0xff000000),
        appBar: AppBar(
          backgroundColor: const Color(0xff000000),
          title: const Text('添加收货地址'),
          // leadingWidth: 8.w,
          leading: GestureDetector(
            onTap: () {
              SmartDialog.dismiss(tag: _dialogTagByAddress);
            },
            child: Center(
              child: Image.asset(
                'assets/images/icons/base_ic_back_white.png',
                width: 12.w,
              ),
            ),
          ),
        ),
        body: Column(
          children: [
            Expanded(
                child: Padding(
              padding: EdgeInsets.symmetric(horizontal: 14.w),
              child: Column(
                children: [
                  Row(
                    children: [
                      SizedBox(width: 80.w, child: const Text('收货人')),
                      Expanded(
                          child: Container(
                              height: 29.w,
                              decoration: BoxDecoration(
                                color: const Color(0xffffffff),
                                borderRadius: BorderRadius.circular(4.w),
                              ),
                              child: TextField(
                                decoration: InputDecoration(
                                  hintText: '请输入收货人',
                                  hintStyle: TextStyle(
                                    fontSize: 12.w,
                                    color: const Color(0xff999999),
                                  ),
                                  border: InputBorder.none,
                                  contentPadding: EdgeInsets.zero,
                                ),
                              ))),
                    ],
                  ),
                  Row(
                    children: [
                      SizedBox(width: 80.w, child: const Text('手机号码')),
                      const Expanded(child: TextField()),
                    ],
                  ),
                  Row(
                    children: [
                      SizedBox(width: 80.w, child: const Text('填写地址')),
                      const Expanded(child: TextField()),
                    ],
                  ),
                  Row(
                    children: [
                      SizedBox(width: 80.w, child: const Text('详细地址')),
                      const Expanded(child: TextField()),
                    ],
                  ),
                ],
              ),
            )),
            GestureDetector(
              onTap: () {
                // SmartDialog.dismiss(tag: _dialogTagByAddress);
              },
              child: const Text('立即购买'),
            )
          ],
        ),
      ),
    );
  }

  Widget _buildTitle() {
    return Text(
      widget.originalFlavor.title ?? '',
      maxLines: 2,
      overflow: TextOverflow.ellipsis,
      style: TextStyle(
        fontSize: 12.w,
        color: Colors.white,
      ),
    );
  }

  Widget _buildPirce() {
    Color color = const Color(0xFFF43B75);
    return Container(
      width: 97.w,
      height: 32.w,
      decoration: BoxDecoration(
        // color: color,
        borderRadius: BorderRadius.circular(22.w),
        border: Border.all(
          color: color,
          width: 1.w,
        ),
      ),
      child: Center(
        child: Text(
          '${widget.originalFlavor.price}金币',
          style: TextStyle(
            fontSize: 14.sp,
            color: color,
          ),
        ),
      ),
    );
  }

  Widget _buildActionByStatus0() {
    return GestureDetector(
      onTap: () {
        SmartDialog.show(
          builder: (context) => _buildConfirmDialogContent(),
          tag: _dialogTagByConfirm,
          alignment: Alignment.bottomCenter,
        );
      },
      child: Container(
        width: double.infinity,
        height: 32.w,
        decoration: BoxDecoration(
          color: const Color(0xFFF5427B),
          borderRadius: BorderRadius.circular(22.w),
        ),
        child: Center(
          child: Text(
            '立即购买',
            style: TextStyle(
              fontSize: 14.w,
              color: Colors.white,
            ),
          ),
        ),
      ),
    );
  }

  Widget _buildActionByStatus1() {
    return Container(
      width: double.infinity,
      height: 32.w,
      decoration: BoxDecoration(
        color: const Color(0xFF343434),
        borderRadius: BorderRadius.circular(22.w),
      ),
      child: Center(
        child: Text(
          '已卖出',
          style: TextStyle(
            fontSize: 14.w,
            color: Colors.white,
          ),
        ),
      ),
    );
  }

  Widget _buildSeller() {
    return Row(
      mainAxisAlignment: MainAxisAlignment.start,
      crossAxisAlignment: CrossAxisAlignment.center,
      children: [
        SizedBox(
          height: 32.w,
          width: 32.w,
          child: ClipRRect(
            borderRadius: BorderRadius.circular(16.w),
            child: ImageView(
              src: widget.originalFlavor.belongsLogo ?? '',
              width: 32.w,
              height: 32.w,
            ),
          ),
        ),
        SizedBox(width: 8.w),
        Text(widget.originalFlavor.belongsNickname ?? '****',
            style: TextStyle(
              fontSize: 12.w,
              color: Colors.white,
            )),
      ],
    );
  }

  Widget _buildPreview() {
    return GestureDetector(
      onTap: () {},
      child: ClipRRect(
        borderRadius: BorderRadius.circular(6.w),
        child: Stack(
          children: [
            ImageView(
              src: widget.originalFlavor.images?[0] ?? '',
              width: double.infinity,
              height: double.infinity,
            ),
            if (widget.originalFlavor.isOrigin == true)
              Positioned(
                top: 1.w,
                left: 2.w,
                width: 54.w,
                height: 24.w,
                child:
                    Image.asset('assets/images/ic_original_flavor_origin.png'),
              ),
            if (widget.originalFlavor.videoCert == true)
              Positioned(
                top: 44.5.w,
                left: 37.5.w,
                width: 30.w,
                height: 30.w,
                child: Image.asset('assets/images/bg_play_icon.png'),
              ),
            Positioned(
              right: 4.w,
              bottom: 4.w,
              child: Container(
                width: 35.w,
                height: 19.w,
                decoration: BoxDecoration(
                  color: Colors.black.withOpacity(0.3),
                  borderRadius: BorderRadius.circular(12.w),
                ),
                child: Center(
                  child: Text(
                    '+${widget.originalFlavor.images?.length ?? 0}',
                    style: TextStyle(
                      fontSize: 12.w,
                      color: Colors.white,
                    ),
                  ),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Row(
      crossAxisAlignment: CrossAxisAlignment.end,
      mainAxisSize: MainAxisSize.max,
      children: [
        SizedBox(
          width: 105.w,
          height: 119.w,
          child: _buildPreview(),
        ),
        SizedBox(width: 10.w),
        Expanded(
          child: SizedBox(
            height: 119.w,
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              mainAxisSize: MainAxisSize.max,
              children: [
                _buildTitle(),
                SizedBox(height: 10.w),
                _buildSeller(),
                SizedBox(height: 10.w),
                Row(
                  children: [
                    _buildPirce(),
                    SizedBox(width: 17.w),
                    Expanded(
                      child: widget.originalFlavor.status == 0
                          ? _buildActionByStatus0()
                          : _buildActionByStatus1(),
                    ),
                  ],
                )
                // _buildAction(),
              ],
            ),
          ),
        )
      ],
    );
  }
}
